//
//  UserDataManager.m
//  trongvm
//
//  Created by Phan Ba Minh on 6/5/12.
//  Copyright (c) 2012 kidbaw. All rights reserved.
//

#import "UserDataManager.h"
#import "Define.h"
//#import "KardsDataManager.h"
#import "AutomationDataManager.h"

@implementation UserDataManager

#pragma mark - Encode - Decode

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeBool:self.loginStatus forKey:STRING_CODER_USER_DATA_LOGIN_STATUS];
    [encoder encodeObject:self.userName forKey:STRING_CODER_USER_DATA_USER_NAME];
    [encoder encodeObject:self.userID forKey:STRING_CODER_USER_DATA_USER_ID];
    [encoder encodeObject:self.sessionID forKey:STRING_CODER_USER_DATA_SESSION_ID];
	[encoder encodeObject:self.password forKey:STRING_CODER_USER_DATA_PASSWORD];
    [encoder encodeBool:self.isLoginWith_FB_TW forKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW];
    [encoder encodeObject:self.avatarSmallURL forKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL];
    [encoder encodeObject:self.userFullName forKey:STRING_CODER_USER_DATA_FULL_NAME];
    [encoder encodeInteger:self.idProfile forKey:STRING_CODER_USER_DATA_USER_PROFILE_ID];
    [encoder encodeObject:self.userLocation forKey:STRING_CODER_USER_DATA_USER_LOCATION];
    [encoder encodeObject:self.currentPlaceMark forKey:STRING_CODER_USER_DATA_CURRENT_PLACE_MARK];
    [encoder encodeObject:self.userEmail forKey:STRING_CODER_USER_DATA_USER_EMAIL];
    [encoder encodeInteger:self.userRequestStatus forKey:STRING_CODER_USER_DATA_USER_REQUEST_STATUS];
    [encoder encodeObject:self.selectedPersonID forKey:STRING_CODER_USER_DATA_SELECTED_PERSON_ID];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        if ([decoder decodeBoolForKey:STRING_CODER_USER_DATA_LOGIN_STATUS]) self.loginStatus = [decoder decodeBoolForKey:STRING_CODER_USER_DATA_LOGIN_STATUS];
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NAME]) self.userName = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NAME];
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_ID]) self.userID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_ID];
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_SESSION_ID]) self.sessionID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_SESSION_ID];
		if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_PASSWORD]) self.password = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_PASSWORD];
        if ([decoder decodeBoolForKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW]) self.isLoginWith_FB_TW = [decoder decodeBoolForKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW];
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL]) self.avatarSmallURL = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL];
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_FULL_NAME]) self.userFullName = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_FULL_NAME];
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_USER_PROFILE_ID]) {
            self.idProfile = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_USER_PROFILE_ID];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_CURRENT_PLACE_MARK]) {
            self.currentPlaceMark = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_CURRENT_PLACE_MARK];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_EMAIL]) self.userEmail = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_EMAIL];
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_USER_REQUEST_STATUS]) {
            self.userRequestStatus = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_USER_REQUEST_STATUS];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_SELECTED_PERSON_ID]) self.selectedPersonID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_SELECTED_PERSON_ID];
    }
    return self;
}

- (void)setAvatarSmallURL:url {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:[NSURL URLWithString:url] delegate:self];
    _avatarSmallURL = url;
}

#pragma mark - SDWebImageDownloaderDelegate

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
    //    NSLog(@"PhotoShopObject; webImageManager; didFinishWithImage");
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error {
    //    NSLog(@"PhotoShopObject; webImageManager; didFailWithError");
}

#pragma mark - Method

- (void)cleanUserCacheAndCookie { 
    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [cookies deleteCookie:cookie];
    }
    // ************************* //
}

- (void)resetAllData {
    // reset profile data
    [UserDataManager Shared].userID = @"";
    [UserDataManager Shared].sessionID = @"";
    [UserDataManager Shared].userName = @"";
    [UserDataManager Shared].loginStatus = NO;
    [UserDataManager Shared].isLoginWith_FB_TW = NO;
    [UserDataManager Shared].avatarSmallURL = @"";
    [UserDataManager Shared].userFullName = @"";
    [UserDataManager Shared].userEmail = @"";
    [UserDataManager Shared].userRequestStatus = 0;
    
    [self cleanUserCacheAndCookie];
}

- (NSString*)getUserPlaceMark {
    [[AutomationDataManager Shared] startUpdateLocationService];
    if (self.currentPlaceMark.locality == nil) {
        return self.currentPlaceMark.administrativeArea;
    }
    return self.currentPlaceMark.locality;
}

- (void)save {
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self] forKey:STRING_USER_DEFAULT_USER_DATA_MANAGER];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

static UserDataManager *m_Instance;
+ (UserDataManager *)Shared
{
    if ( !m_Instance ) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:STRING_USER_DEFAULT_USER_DATA_MANAGER];
        if ([NSKeyedUnarchiver unarchiveObjectWithData:data]) {
            m_Instance = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        
        if (m_Instance == nil) {
            m_Instance = [UserDataManager new];
        }
    }
    return m_Instance;
}

@end
